<?php

namespace app\api\Service;

use think\Db;
use think\Config;
use app\common\library\Auth;

class StudentService
{
    // 新增
    public static function add($data)
    {
        try{
            $auth = Auth::instance();
            $data['user_id'] = $auth->id;
            $data['gender'] = isset($data['gender']) ? $data['gender'] : 0;
            if(!$data['name']) return ['code'=>0, 'msg'=>'请填写姓名','data'=>''];
            if( $data['gender'] != 1 && $data['gender'] != 0 ) return ['code'=>0, 'msg'=>'请选择性别','data'=>''];
            if(!$data['nation']) return ['code'=>0, 'msg'=>'请选择民族','data'=>''];
            if(!$data['school']) return ['code'=>0, 'msg'=>'请填写所在学校','data'=>''];
            if(!$data['grade']) return ['code'=>0, 'msg'=>'请填写所在年级','data'=>''];
            if(!$data['school_region']) return ['code'=>0, 'msg'=>'请选择学校地区','data'=>''];
            if(!$data['school_addr']) return ['code'=>0, 'msg'=>'请填写学校地址','data'=>''];
            if(!$data['school_lng'] || !$data['school_lat'] ) return ['code'=>0, 'msg'=>'学校经纬度错误','data'=>''];
            if(!$data['home_region']) return ['code'=>0, 'msg'=>'请选择家庭地区','data'=>''];
            if(!$data['home_addr']) return ['code'=>0, 'msg'=>'请填写家庭地址','data'=>''];
            if(!$data['home_lng'] || !$data['home_lat'] ) return ['code'=>0, 'msg'=>'家庭经纬度错误','data'=>''];
            if(!$data['urgent_contact']) return ['code'=>0, 'msg'=>'请填写紧急联系人','data'=>''];

            // 把其他地址设为不是默认
            if($data['is_default'] == 1){
                \app\admin\model\Student::where(['user_id'=>$data['user_id']])->update(['is_default'=>0]);
            }

            $params = [
                'user_id' => $data['user_id'],
                'name' => $data['name'],
                'gender' => $data['gender'],
                'nation' => $data['nation'],
                'school' => $data['school'],
                'grade' => $data['grade'],
                'school_region' => $data['school_region'],
                'school_addr' => $data['school_addr'],
                'school_lng' => $data['school_lng'],
                'school_lat' => $data['school_lat'],
                'home_region' => $data['home_region'],
                'home_addr' => $data['home_addr'],
                'home_lng' => $data['home_lng'],
                'home_lat' => $data['home_lat'],
                'urgent_contact' => htmlspecialchars_decode($data['urgent_contact']),
                'is_default' => $data['is_default'],
            ];

            $result = \app\admin\model\Student::create($params);

            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            return ['code'=>0,'message'=>$e->getMessage(),'data'=>''];
        }
        if($result){
            return ['code'=>1,'msg'=>'操作成功','data'=>''];
        }else{
            return ['code'=>0,'msg'=>'操作失败','data'=>''];
        }
    }

    // 详情
    public static function details($data){
        $id = $data['id'];

        if(!$id) return ['code'=>0,'msg'=>'无效参数','data'=>''];
        $details = \app\admin\model\Student::where(['id'=>$id])->field('createtime',true)->find();
        $details['urgent_contact'] = json_decode($details['urgent_contact'],true);
//        return $details['urgent_contact'];
        return ['code'=>1,'msg'=>'查询成功','data'=>$details];
    }

    // 编辑
    public static function edit($data)
    {
        try{
            $auth = Auth::instance();
            $data['user_id'] = $auth->id;
            $data['gender'] = isset($data['gender']) ? $data['gender'] : 0;
            if(!$data['id']) return ['code'=>0, 'msg'=>'无效参数','data'=>''];
            if(!$data['name']) return ['code'=>0, 'msg'=>'请填写姓名','data'=>''];
            if( $data['gender'] != 1 && $data['gender'] != 0 ) return ['code'=>0, 'msg'=>'请选择性别','data'=>''];
            if(!$data['nation']) return ['code'=>0, 'msg'=>'请选择民族','data'=>''];
            if(!$data['school']) return ['code'=>0, 'msg'=>'请填写所在学校','data'=>''];
            if(!$data['grade']) return ['code'=>0, 'msg'=>'请填写所在年级','data'=>''];
            if(!$data['school_region']) return ['code'=>0, 'msg'=>'请选择学校地区','data'=>''];
            if(!$data['school_addr']) return ['code'=>0, 'msg'=>'请填写学校地址','data'=>''];
            if(!$data['school_lng'] || !$data['school_lat'] ) return ['code'=>0, 'msg'=>'学校经纬度错误','data'=>''];
            if(!$data['home_region']) return ['code'=>0, 'msg'=>'请选择家庭地区','data'=>''];
            if(!$data['home_addr']) return ['code'=>0, 'msg'=>'请填写家庭地址','data'=>''];
            if(!$data['home_lng'] || !$data['home_lat'] ) return ['code'=>0, 'msg'=>'家庭经纬度错误','data'=>''];
            if(!$data['urgent_contact']) return ['code'=>0, 'msg'=>'请填写紧急联系人','data'=>''];

            // 把其他地址设为不是默认
            if($data['is_default'] == 1){
                \app\admin\model\Student::where(['user_id'=>$data['user_id']])->update(['is_default'=>0]);
            }

            $params = [
                'user_id' => $data['user_id'],
                'name' => $data['name'],
                'gender' => $data['gender'],
                'nation' => $data['nation'],
                'school' => $data['school'],
                'grade' => $data['grade'],
                'school_region' => $data['school_region'],
                'school_addr' => $data['school_addr'],
                'school_lng' => $data['school_lng'],
                'school_lat' => $data['school_lat'],
                'home_region' => $data['home_region'],
                'home_addr' => $data['home_addr'],
                'home_lng' => $data['home_lng'],
                'home_lat' => $data['home_lat'],
                'urgent_contact' => htmlspecialchars_decode($data['urgent_contact']),
                'is_default' => $data['is_default'],
            ];

            $result = \app\admin\model\Student::where('id',$data['id'])->update($params);

            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            return ['code'=>0,'message'=>$e->getMessage(),'data'=>''];
        }
        if($result){
            return ['code'=>1,'msg'=>'操作成功','data'=>''];
        }else{
            return ['code'=>0,'msg'=>'操作失败','data'=>''];
        }
    }

    // 默认
    public static function default($data)
    {
        try{
            $auth = Auth::instance();
            $data['user_id'] = $auth->id;
            if(!$data['id']) return ['code'=>0, 'msg'=>'无效参数','data'=>''];

            // 把所有地址设为非默认
            \app\admin\model\Student::where(['user_id'=>$data['user_id']])->update(['is_default'=>0]);
            // 把指定地址设为默认
            $result = \app\admin\model\Student::where(['user_id'=>$data['user_id']])->where('id',$data['id'])->update(['is_default'=>1]);

            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            return ['code'=>0,'message'=>$e->getMessage(),'data'=>''];
        }
        if($result){
            return ['code'=>1,'msg'=>'操作成功','data'=>''];
        }else{
            return ['code'=>0,'msg'=>'操作失败','data'=>''];
        }
    }

    // 列表
    public static function list($data)
    {
        $limit = isset($data['limit']) ? $data['limit'] : 10;
        $user_id = $data['user_id'];

        $list = \app\admin\model\Student::where(['user_id'=>$user_id])->order('is_default desc,createtime desc')->field
        ('id,name,school_region,school_addr,home_region,home_addr,is_default')->paginate($limit)->toArray();
        foreach($list['data'] as &$item){

            $item['school_address'] = $item['school_region'] . $item['school_addr'];
            $item['home_address'] = $item['home_region'] . $item['home_region'];
            unset($item['school_region']);
            unset($item['school_addr']);
            unset($item['home_addr']);
            unset($item['home_region']);

        }
        return ['code'=>1,'msg'=>'查询成功','data'=>['total'=>$list['total'],'list'=>$list['data']]];
    }

    // 删除
    public static function del($data)
    {
        $id = $data['id'];
        $user_id = $data['user_id'];

        try {
            $result = \app\admin\model\Student::where(['id'=>$id,'user_id'=>$user_id])->delete();
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            return ['code'=>0,'message'=>$e->getMessage(),'data'=>''];
        }
        if($result){
            return ['code'=>1,'msg'=>'操作成功','data'=>''];
        }else{
            return ['code'=>0,'msg'=>'操作失败','data'=>''];
        }
    }
}